package dse.modules.eln;

import java.util.Map;

import dse.DseException;
import dse.Network;
import dse.Node;
import dse.ExperimentConfiguration;
import dse.modules.CommunicationCostCalculator;

public class EarthLikeNetworkCostCalculator 
implements CommunicationCostCalculator
{   
	public void initalize (
			ExperimentConfiguration systemSettings, 
			Map<String, String> moduleSettings
			)
	throws DseException
	{
		// Nothing really needed at this point...
	}
	
	public double cost(Network network, Node from, Node to, long size)
	{
		double distance = distance(network, from, to);
		//double pingCostInMS = 0.5 + 0.18*Math.pow(distance, 0.74);
		//double latency = (0.05*Math.pow(size, 0.46)*pingCostInMS + pingCostInMS)/1000;
		//double mu = 1.0/0.3;
		//double sign = Math.random()<0.5?-1:1;
		//double error = -Math.log(1-Math.random())/mu;
		double pingCostInMS = 0.4 + 0.3*Math.pow(distance, 0.735);
		double latency = (0.02*Math.pow(size, 0.51)*pingCostInMS + pingCostInMS)/1000;
		//double mu = 1.0/0.3;
		//double sign = Math.random()<0.5?-1:1;
		//double error = -Math.log(1-Math.random())/mu;
		return latency;// + latency*sign*error;
	}
	
	public double distance (Network network, Node from, Node to)
	{	
		NodeWithCoordinates n1 = (NodeWithCoordinates)from;
		NodeWithCoordinates n2 = (NodeWithCoordinates)to;
		
		double distance = GeometryUtil.distance(
				n1.getX(), n1.getY(), n2.getX(), n2.getY()
				);
		
		if (distance == 0)
			return 0.1;
		return distance;
	}
}
